<?
    cria_variaveis($_POST,$_GET,$_FILES); 
?>
<p class="titulo" align="center"><?php echo $msg->get('DECMAT_MSG_02') ?></p>
<p class="subtitulo" align="center"><?php echo $msg->get('DECMAT_MSG_03') ?> <a href="#" onClick="window.open('info.php?topico=decmat', 'Informacoes', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=400,height=320')"><img src="imagens/info.gif" border="0"></a></p>
<p>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=decmat.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get('Material_Teorico') ?></a></li>
    <li><a href="index.php?inc=decmat.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplos_Prontos') ?></a></li>
    <li><a href="index.php?inc=decmat.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get('Entrada_Teclado') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
if (!isset($Calcular)) {	
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material")
    {
        require_once("./classes/upload.php");
        $uploads = new Upload();
        $uploads->Lista("matriz");
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") { //leitura de dados via banco de dados - modulo de exemplos e exercicios
        include "exibe_prob_matriz_decmat_xml.php";
	}
	else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") {
        if ((strlen($dim)==0) | ($dim<0))  { 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=decmat.php>";
            echo $msg->get('DECMAT_MSG_04');
            echo "<input type=text size='2' maxlength='2' name=dim value=".($dim).">";
            echo "<br><br>";
                echo "<input type=hidden name=executar value=sim>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_02').">";

                echo "</form>"; 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=decmat.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        }
        elseif (!checaNumerico($dim))
        {
    ?>
            <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
            <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
    <?php
        }
        else  {
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=decmat.php>";
            //echo DECMAT_MSG_04;
            echo "<input type=hidden name=dim value=".($dim).">";
            echo "<br><br>";
            echo "<p align=left><font face=Arial>".$msg->get('DECMAT_MSG_05')."</font>";
            echo "<nobr>";
            for ($i=0; $i<$dim; $i++) {
            echo "<br><br>";
            for ($j=0; $j<$dim; $j++) {
                    echo "<input type=text name=A[".($i)."][".($j)."] maxlength=10 size=5 value=".($A[$i][$j]).">";
            } //end forj
            } //end fori
            echo "</nobr>";
            echo "<p>";
            echo "<input class=button type=submit name=Calcular value=".$msg->get('G_MSG_04').">";
            echo "<br>";
            echo "</form>";
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=decmat.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        } //end if
        echo "</body>";
    }
} else {
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo")
        verificaBib($xml_file);
    if (!checaNumerico($dim) || !checaNumerico($A))
    {
?>
        <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
        <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
    }
    else
    {
        echo "<br>";
        echo "<b>".$msg->get('DECMAT_MSG_07')."<br> <br> </b>";
        for ($aux=1; $aux < $dim; $aux++)
        {
            if ($aux == 1) //primeira submatriz (1x1);
            {
                $result[0] = $A[0][0];
            }
            else if ($aux == 2) //segunda submatriz (2x2);
            {
                $result[1] = $A[0][0]*$A[1][1] - $A[0][1]*$A[1][0];
            }
            else
            { // para as submatrizes maiores que 2x2
                for ($i=0; $i<$aux; $i++)
                    for ($j=0; $j<$aux; $j++)
                        $B[$i][$j]=$A[$i][$j];
                $d=1;
                for ($k=0; $k<$aux; $k++)
                {
                    $p=$B[$k][$k];
                    for ($i=0; $i<$aux; $i++)
                    {
                        $t[$i]=$B[$i][$k];
                    }
                    for ($j=$k; $j<$aux ;$j++)
                    {
                        $B[$k][$j]/=$p;
                        for ($i=0; $i<$aux; $i++)
                        {
                            if ($k!=$i) { $B[$i][$j]-=$t[$i]*$B[$k][$j]; }
                        } // end for i
                    } //end for j
                    $d = $d * $p; //fator de correção do determinante
                } //end for k
                for ($i=0; $i<$aux; $i++)
                    $d*=$B[$i][$i];
                $result[$aux-1] = $d;
            } //end else
        } //end for aux
        
        echo "\n<script language=\"javascript\" type=\"text/javascript\">\n";
        for ($k=1; $k<=$dim-1; $k++)
        {
        echo "function formulario$k() {\n";
        echo "window.open('calcdet.php', 'submatriz$k', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=400,height=320');\n";
        echo "document.matriz$k.submit();\n";
        echo "}\n";
        }
        echo "</script>";
        $positiva = 0;
        for ($k=1; $k<=($dim-1); $k++) { ?> <br> <?
            if ($result[$k-1] == 0)
                { $positiva = 1;}
            if ($k < $aux) {
                echo "".$msg->get('DECMAT_MSG_11')." A<sub>$k</sub>: ";
            }
            echo "<br><br>";
            echo "<form action=calcdet.php method=post name=matriz$k target=submatriz$k><table>";
            for ($i=0; $i<$dim ; $i++)
            {
                //echo "<br>";
                echo "<tr>";
                for ($j=0; $j<$dim ; $j++)
                {
                    if (($i<$k)and($j<$k))
            {
                        echo "<td bgcolor=#FFCFAB weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
                    echo "<input type=hidden name=A[$i][$j] value=".$A[$i][$j].">";
            }
                    else
                        echo "<td weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
    //			       echo "<input type=text size=5 maxlength=5 name=A[".($i)."][".($j)."] value= ".($A[$i][$j]).">";
            echo $A[$i][$j];
                    echo "</td>";
                } //end for
                echo "</tr>";
            } //end for
            echo "<input type=hidden name=dim value=$k>";
            echo "</table></form>";
            echo "<br><a href=\"javascript:void(0)\" onClick=\"formulario$k()\">";
            if ($k < $aux) {
                echo "<b>".$msg->get('DECMAT_MSG_12')." $k x $k = </b></a>";
                if ($result[$k-1]!=0)
                    echo "<font color=blue>".$result[$k-1]."</font>";
                else
                    echo "<font color=red>".$result[$k-1]."</font>";
            }
            echo "<br>";
    
        } // end for
        echo "<br>";
        if ($positiva == 0)
            { echo $msg->get('DECMAT_MSG_09'); }
        else { echo $msg->get('DECMAT_MSG_10');}
        if (isset($correcao) && $correcao=="sim") {
            echo "<br> <br>";
            echo "<b>".$msg->get('DECMAT_MSG_06')."<br> <br>  </b>";
            echo "".$msg->get('DECMAT_MSG_08')."<br> <br>";
            for ($i=0; $i<$dim; $i++) { 
                echo "".$msg->get('DECMAT_MSG_11')."".($i+1)."x".($i+1)." - <input type=text name=determinante[".($i)."] maxlength=10 size=5 value=".($determinante[$i]).">";
                echo "<br>";
            } //end for
            echo "<br>";
            if ($resp==0) { echo $msg->get('DECMAT_MSG_10'); }
            if ($resp==1) { echo $msg->get('DECMAT_MSG_09'); }
        } // end if
        echo "<p>";
        echo "</form>";
        if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo")
        {
            echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
            echo "<script language=\"javascript\" type=\"text/javascript\">\n";
            echo "function criarLinks(value) {\n";
            echo "document.exemplos.indice.value = value;\n";
            echo "document.exemplos.action = 'index.php?inc=decmat.php&modo=exemplo'\n";
            echo "document.exemplos.submit();\n";
            echo "}\n";
            echo "</script>";
            echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=\"hidden\" name=\"indice\">";
            echo $msg->get('Outros_Exemplos')."<br>";
            $objeto = new DomDocument();
            $objeto->load($xml_file);
            $filho = $objeto->getelementsbytagname("matrices");
            if ($filho->length > 0)
            {
                $temp = $filho->item(0)->getelementsbytagname("matrix");
                $j = 0;
                for($i = 0; $i < $temp->length; $i++)
                {
                    $dim = $temp->item($i)->getelementsbytagname("dim");
                    if ($dim->length > 0)
                    {
                        echo ($j == 0) ? '' : ' | ';
                        echo "<a class=\"bibliotecaexemplos\" href=\"javascript:criarLinks(".$j.")\" title=\"";
                        echo "Matriz dimens&atilde;o ".$dim->item(0)->nodeValue."\">";
                        echo ($j == $indice)?"<b><u>":"<u>";
                        echo ($j+1);
                        echo ($j == $indice)?"</b></u>":"</u>";
                        echo "</a>";
                        $j++;
                    }
                }
            }
            echo "<br /> <br />";
        }
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=decmat.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
        echo "</body>";
    }
}
?>
</div>
